package com.qiang.work.test01;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;


public class MyThread implements Runnable {
    private static final Logger LOGGER = LoggerFactory.getLogger(Demo02.class);
    /*public MyThread(Socket accept) {

    }*/
    private Socket accept;

    public MyThread(Socket accept) {
        this.accept = accept;
    }


    @Override
    public void run() {

        //网络中的流,从客户端读取数据的
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(accept.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        //本地的IO流,把数据写到本地中,实现永久化存储
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream("D:\\dd\\" + UUID.randomUUID().toString() + ".jpg"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        try {
            int b;
            while ((b = bis.read()) != -1) {
                bos.write(b);
            }

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
            bw.write("上传成功");
            bw.newLine();
            bw.flush();

            bos.close();
            accept.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //ss.close();
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String format = df.format(date);
        InetAddress inetAddress = accept.getInetAddress();
        String hostAddress = inetAddress.getHostAddress();
        LOGGER.warn(format);
        LOGGER.warn(hostAddress);
        LOGGER.warn("发送成功");

    }
}
